package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.domain.vo.CategoryBrandVO;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.CategoryBrandService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper  , CategoryBrand>
        implements CategoryBrandService {
    @Override
    public List<CategoryBrandVO> listCategoryBrands(CategoryBrand categoryBrand) {
        return baseMapper.listCategoryBrands(categoryBrand);
    }

    @Override
    public void checkCategoryBrandUnique(CategoryBrand categoryBrand) {
        long id = categoryBrand.getId() == null ? -1L : categoryBrand.getId();
        //使用品牌id+分类id 组合查询分类品牌数据
        CategoryBrand dbcategoryBrand = baseMapper.selectOne(Wrappers.lambdaQuery(CategoryBrand.class)
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId())
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
                .last("limit 1"));
        if (dbcategoryBrand != null && dbcategoryBrand.getId().longValue() != id) {
            throw new ServiceException("分类已存在该品牌");
        }
    }

    @Override
    public boolean deleteBatchByIds(List<Long> ids) {
        return baseMapper.deleteBatchByIds(ids);
    }
}
